﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.Data.Entity;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_NoiDungSatHachController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        //
        // GET: /GPS_HTCapGPLX/
        #region Index
        public ActionResult Index(ViewModelSNOIDUNGSH SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SNOIDUNGSH>(o => (SearchModel.nNDSH_MA == null || o.NDSH_MA.StartsWith(SearchModel.nNDSH_MA))
                                        && (SearchModel.nNDSH_TEN == null || o.NDSH_TEN.StartsWith(SearchModel.nNDSH_TEN))
                                        && (SearchModel.nNDSH_ACTIVE == null || o.NDSH_ACTIVE == SearchModel.nNDSH_ACTIVE));
            ViewBag.TotalRow = entities.Count();
            var model = new List<SNOIDUNGSH>();

            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<SNOIDUNGSH>(currentEntity));
            }
            Int16 icount = 1;
            foreach (var item in model)
            {
                item.STT = icount;
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSNOIDUNGSH viewModel)
        {
            //ViewBag.lstActivedll = new SelectList(_db.GetAll<STRANGTHAI>(), "TRANGTHAI_MA", "TRANGTHAI_TEN");
            //viewModel.listTrangThai = _db.GetAll<STRANGTHAI>();
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelSNOIDUNGSH());
        }

        [HttpPost]
        public ActionResult Create(CrudModelSNOIDUNGSH model)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SNOIDUNGSH _model = new SNOIDUNGSH();
                GPS.COMMON.Helpers.CopyObject<SNOIDUNGSH>(model, ref _model);
                _model.NDSH_MA = GetValue_Indentity();
                _db.Insert<SNOIDUNGSH>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            // ViewBag.lstActivedll = new SelectList(_db.GetAll<STRANGTHAI>(), "TRANGTHAI_MA", "TRANGTHAI_TEN");
        }

        public string GetValue_Indentity()
        {
            string value = "";
            Int64 valueMax = _db.DbContext.SNOIDUNGSHes.ToList().Max(e => Convert.ToInt64(e.NDSH_MA));
            value = (valueMax + 1).ToString();
            return value;
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string id)
        {
            CrudModelSNOIDUNGSH _model = new CrudModelSNOIDUNGSH();
            SNOIDUNGSH model = _db.GetOne<SNOIDUNGSH>(o => o.NDSH_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSNOIDUNGSH>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSNOIDUNGSH model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SNOIDUNGSH>(o => o.NDSH_MA == model.NDSH_MA);
                GPS.COMMON.Helpers.CopyObject<SNOIDUNGSH>(model, ref _model);
                _db.Update<SNOIDUNGSH>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelSNOIDUNGSH view)
        {
            //string selectedValues = _db.GetAll<STRANGTHAI>().Select(x => x.TRANGTHAI_MA = view.LYDO_ACTIVE).FirstOrDefault().ToString();
            //ViewBag.lstActivedll = new SelectList(_db.DbContext.STRANGTHAIs.ToList(), "TRANGTHAI_MA", "TRANGTHAI_TEN", selectedValues);
        }
        #endregion

        #region Detele
        public ActionResult Delete(string id)
        {
            CrudModelSNOIDUNGSH _model = new CrudModelSNOIDUNGSH();
            SNOIDUNGSH model = _db.GetOne<SNOIDUNGSH>(o => o.NDSH_MA == id);
            _db.DeleteItem<SNOIDUNGSH>(model);
            return RedirectToAction("Index");
        }
        #endregion

        //
        // Detail
        #region Detail
        public ActionResult Detail(string id)
        {
            CrudModelSNOIDUNGSH _model = new CrudModelSNOIDUNGSH();
            SNOIDUNGSH model = _db.GetOne<SNOIDUNGSH>(o => o.NDSH_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelSNOIDUNGSH>(model, ref _model);
            if (_model.NDSH_ACTIVE == "1")
            {
                _model.NDSH_ACTIVE = "Hiệu Lực";
            }
            else
            {
                _model.NDSH_ACTIVE = "Không Hiệu Lực";
            }
            return PartialView("Detail", _model);
        }
        #endregion
    }
}